// pages/supplier/goodsCenter/goodsCenter.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasMore:true,
        pageSize:10,
        pageNo:1,
        status:1,
        tabs: [],
        activeTab: 0,
        orderList:[],
        btnStatus:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const titles = ['待审核', '已审核', '上架中']
        const tabs = titles.map(item => ({title: item}))
        this.setData({tabs})
    },
    /**
     * 生命周期函数--监听页面显示
     */

    onShow(){
        this.getData(this.data.status)
    },
    onPullDownRefresh: function () {
        this.getData(this.data.status)
    },
    handleDelete(e){
        let that = this
        let $id = e.currentTarget.dataset.id
        wx.showModal({
          title: '提示',
          content:'确定删除吗？',
          confirmColor:'#F96A42',
          cancelColor:'#666',
          success (res) {
            if (res.confirm) {
                that.onDelete($id)
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    },
    onDelete(id){
        let that = this;
        wx.showLoading({
            title: '删除中...',
          })
        that.setData({
            btnStatus:true
        })
        util.request(api.supplier.commodityDelete+'?commodityId='+id,{},'put').then(res=>{
            that.setData({
                btnStatus:false
            })
            wx.showToast({
              title: '已删除',
              icon:'success'
            })
            that.getData(that.data.status)
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
            wx.hideLoading()
        })
    },
    handleEdit(e){
        console.log(e.currentTarget.dataset.data)
        let data = JSON.stringify(e.currentTarget.dataset.data)
        wx.navigateTo({
          url: '/pages/supplier/goodsManage/goodsManage?data='+data,
        })
    },
    getData(status){
        let that = this;
        that.setData({
            pageNo: 1,
            hasMore:true,
            orderList:[]
        });
        wx.showLoading({
            title: '加载中...',
          })
        util.request(api.supplier.goodsList,{
            pageNo:that.data.pageNo,
            pageSize:that.data.pageSize,
            status:status
        },'get').then(res=>{
            that.setData({
                orderList: res.result
            });
            if (res.result.length < that.data.pageSize) {
                that.setData({
                    hasMore: false,
                })
            }
            wx.stopPullDownRefresh();
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
            wx.hideLoading()
            wx.stopPullDownRefresh() 
        })
    },
    loadMore: function(status) {
        var that = this;
        if (!this.data.hasMore) return
        wx.showLoading({
            title: '加载中...',
        })
        var url = api.supplier.goodsList + "?pageNo=" + (++that.data.pageNo) + '&pageSize=' + that.data.pageSize+ '&status=' + status;
        util.request(url,'get').then(function(res) {
            if (res.code == 200) {
                that.setData({
                    orderList: that.data.orderList.concat(res.result)
                });
                if (res.result.length < that.data.pageSize) {
                    that.setData({
                        hasMore: false,
                    })
                }
                wx.hideLoading()
            }
        });
    },
    onChange(e) {
        const index = e.detail.index
        this.setData({
            activeTab: index
        })
    },
    switchTab(e){
        const id = e.currentTarget.dataset.id;
        this.setData({
            activeTab: id
        })
        if(this.data.activeTab === 0){
            this.setData({
                status:1
            })
        } else if(this.data.activeTab === 1){
            this.setData({
                status:2
            })
        } else if(this.data.activeTab === 2){
            this.setData({
                status:3
            })
        }
        this.getData(this.data.status)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getData(this.data.status)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        //console.log('eee')
        this.loadMore(this.data.status)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})